import { GlobalData } from "./GlobalData";

interface GmaeDataInfo {
    coin: number,
    power: number,
    bestscore: number,
    basket: number,
    gametime: number,
    starday: number,
    catlevel: number,
    gunlevel: number,
    currentlevel: number
}

/**
 * 游戏数据
 */
export class GameData {

    private constructor() { }

    /** 单例实例 */
    public static readonly instance: GameData = new GameData();

    //本地缓存Key
    private GameDateKey: string = "GameDateKey";

    //游戏数据
    public GameData: GmaeDataInfo = null;

    //发炮时间
    public FireTime: number = 1;

    //默认游戏数据
    private DefaultGameData: GmaeDataInfo = {
        coin: 0,
        power: 0,
        bestscore: 0,
        basket: 0,
        gametime: 0,
        starday: 0,
        catlevel: 1,
        gunlevel: 1,
        currentlevel: 0
    };

    /**
     * 游戏初始化
     */
    public Init() {
        if (app.dataManager.GetStorage(this.GameDateKey)) {
            this.GameData = app.dataManager.GetStorage(this.GameDateKey);

            if (this.GameData.starday != new Date().getDate()) {
                this.GameData.starday = new Date().getDate();
                this.GameData.power = 5;
                this.UpdateGameDateLocal();
            }

        } else {
            this.GameData = this.DefaultGameData;
            this.GameData.starday = new Date().getDate();
            this.GameData.power = 5;
            this.UpdateGameDateLocal();
        }
    }

    /**
     * 游戏结束
     */
    public GameOver() {
        this.GameData.coin += GlobalData.GameCoin;
        this.GameData.bestscore = Math.max(this.GameData.bestscore, GlobalData.GameScore);
        this.UpdateGameDateLocal();
    }

    /**
     * 更新本地缓存
     */
    public UpdateGameDateLocal() {
        app.dataManager.SetStorage(this.GameDateKey, this.GameData);
    }

    /**
     * 增加体力
     */
    public AddPower(_power: number) {
        this.GameData.power += _power;
        if (this.GameData.power >= 5) {
            this.GameData.power = 5;
            this.GameData.gametime = 0;
        }
        this.UpdateGameDateLocal();
    }
}

export const gameData = GameData.instance;



